/**    
 * 文件名：FirstBillPamentManager.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年7月6日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.service.bill.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.exception.BusinessException;
import com.logic.landseaserver.common.LandeaConstants;
import com.logic.landseaserver.common.LandeaConstants.BillType;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LandSeaUtil;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.domain.RealBill;
import com.logic.landseaserver.persistence.read.RealBillReadMapper;
import com.logic.landseaserver.service.bill.PaymentManager;
import com.logic.landseaserver.ws.dto.WeBankPayReqDTO;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：FirstBillPamentManager</br>
 * 类描述：实时账单类型</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月6日 下午8:10:04</br>
 * 
 * @version 1.0
 *
 */
@Service(LandeaConstants.BILLTYPE_PREFIX + BillType.REALTIME_BILL)
public class RealTimePamentManager implements PaymentManager
{
    private static final Logger LOG = LoggerFactory.getLogger(RealTimePamentManager.class);
    
    @Autowired
    private RealBillReadMapper realBillReadMapper;

    /**
     * [描述]：实时账单不存在校验，只要把实时账单表里的price赋值到payment中</br>
     *
     * @param billPayment
     * @throws LandseaException 如果检查非法会报异常
     *
     */
    @Override
    public Double checkAmountMoney(BillPayment billPayment)
        throws LandseaException
    {
        Integer billId = billPayment.getBillId();
        
        try
        {
            // 实时账单
            RealBill realBill = realBillReadMapper.selectByPrimaryKey(billId);
            if (null == realBill)
            {
                LOG.error("checkAmountMoney|no record in ls_real_bill table with billId=" + billId);
                throw new LandseaBusinessException(LandseaErrorMessageEnum.DEPOSIT_BILL_NOT_EXISTS_NULL);
            }
            
            // 应支付
            Double payMoney = realBill.getBillPrice();
            // 要转换成0.00保留两位小数
            payMoney = LandSeaUtil.get2Double(payMoney);

            billPayment.setMoney(payMoney);
            
            return payMoney;
        }
        catch (Exception e)
        {
            LOG.error("checkAmountMoney|check money failed.", e);
            throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_CHECK_PAYMONEY_ERROR, e);
        }
    }

    /**    
     * [描述]：</br>
     *
     * @param billId
     * @return
     * @throws LandseaException
     *
    */
    @Override
    public Integer getContractId(Integer billId)
        throws LandseaException
    {
        try
        {
            RealBill realBill = realBillReadMapper.selectByPrimaryKey(billId);
            if (null == realBill)
            {
                LOG.error("getContractId|no record in ls_real_bill table with billId=" + billId);
                throw new LandseaBusinessException(LandseaErrorMessageEnum.DEPOSIT_BILL_NOT_EXISTS_NULL);
            }
            return realBill.getContractId();
        }
        catch (Exception e)
        {
            LOG.error("getContractId|query ls_real_bill error with billId=" + billId, e);
            throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_QUERY_BILL_ERROR, e);
        }
    }

    @Override
    public String getPrice(WeBankPayReqDTO payinfo)
        throws LandseaException, BusinessException
    {
        Integer billId = payinfo.getBillId();
        RealBill realBill = realBillReadMapper.selectByPrimaryKey(billId);
        if(null == realBill){
            throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_REALBILL_NULL);
        }
        if(null == realBill.getBillPrice()){
            throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_REALBILL_PRICE_NULL);
        }
        String price = realBill.getBillPrice()+"";
        
        return price;
    }
}
